package com.autumn.zero.workflow.web.controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import com.autumn.common.api.application.dto.input.AdvancedPageQueryInput;
import com.autumn.common.api.application.dto.input.AdvancedQueryInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.web.annotation.IgnoreApiResponseBody;
import com.autumn.zero.workflow.application.dto.WorkflowImageInput;
import com.autumn.zero.workflow.application.dto.WorkflowImageOutput;
import com.autumn.zero.workflow.application.dto.user.task.UserTaskCompleteInput;
import com.autumn.zero.workflow.application.dto.user.task.WorkflowUserTaskCompleteOutput;
import com.autumn.zero.workflow.application.dto.user.task.WorkflowUserTaskDto;
import com.autumn.zero.workflow.application.services.IWorkflowUserTaskAppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 
 * @author 杨昌国 2019-02-25 09:50:48
 */
@RestController
@RequestMapping("/sys/workflow/usertask")
@Api(tags = "工作流用户任务")
public class WorkflowUserTaskController {

	@Autowired
	private IWorkflowUserTaskAppService service;

	/**
	 * 用户待办任务
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/queryTaskWaitByPageList")
	@ApiOperation(value = "用户待办任务")
	@RequiresUser
	public IPageQueryResult<WorkflowUserTaskDto> queryTaskWaitByPageList(
			@Valid @RequestBody AdvancedPageQueryInput input) {
		return service.queryTaskWaitByPageList(input);
	}

	/**
	 * 下载用户待办任务到Excel
	 * 
	 * @param input
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping("/downloadTaskWaitByExcel")
	@ApiOperation(value = "下载用户待办任务到Excel", produces = "application/octet-stream")
	@RequiresUser
	@IgnoreApiResponseBody
	public StreamingResponseBody downloadTaskWaitByExcel(@Valid @RequestBody AdvancedQueryInput input,
			HttpServletResponse response) throws UnsupportedEncodingException {
		String fileName = URLEncoder.encode("用户待办任务.xls", "utf-8");
		response.setContentType("application/xls;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		Workbook workbook = service.exportTaskWaitByExcel(input);
		return outputStream -> {
			try {
				workbook.write(outputStream);
			} finally {
				outputStream.flush();
				outputStream.close();
			}
		};
	}

	/**
	 * 用户已办任务
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/queryTaskCompleteByPageList")
	@ApiOperation(value = "用户已办任务")
	@RequiresUser
	public IPageQueryResult<WorkflowUserTaskCompleteOutput> queryTaskCompleteByPageList(
			@Valid @RequestBody AdvancedPageQueryInput input) {
		return service.queryTaskCompleteByPageList(input);
	}

	/**
	 * 下载用户已办任务到Excel
	 * 
	 * @param input
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping("/downloadTaskCompleteByExcel")
	@ApiOperation(value = "下载用户已办任务到Excel", produces = "application/octet-stream")
	@RequiresUser
	@IgnoreApiResponseBody
	public StreamingResponseBody downloadTaskCompleteByExcel(@Valid @RequestBody AdvancedQueryInput input,
			HttpServletResponse response) throws UnsupportedEncodingException {
		String fileName = URLEncoder.encode("用户已办任务.xls", "utf-8");
		response.setContentType("application/xls;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		Workbook workbook = service.exportTaskCompleteByExcel(input);
		return outputStream -> {
			try {
				workbook.write(outputStream);
			} finally {
				outputStream.flush();
				outputStream.close();
			}
		};
	}

	/**
	 * 用户处理任务
	 * 
	 * @param input 输入
	 */
	@PostMapping("/userTaskComplete")
	@ApiOperation(value = "用户处理任务")
	@RequiresUser
	public void userTaskComplete(@Valid @RequestBody UserTaskCompleteInput input) {
		service.userTaskComplete(input);
	}

	/**
	 * 查询流程图片
	 * 
	 * @param input 输入
	 * @return
	 */
	@PostMapping("/queryByWorkflowImage")
	@ApiOperation(value = "查询流程图片")
	@RequiresUser
	public WorkflowImageOutput queryByWorkflowImage(@Valid @RequestBody WorkflowImageInput input) {
		return service.queryByWorkflowImage(input);
	}

	/**
	 * 查询流程列表
	 * 
	 * @param input 输入
	 * @return
	 */
	@PostMapping("/queryByWorkflowList")
	@ApiOperation(value = "查询流程列表")
	@RequiresUser
	public List<WorkflowUserTaskCompleteOutput> queryByWorkflowList(WorkflowImageInput input) {
		return service.queryByWorkflowList(input);
	}

}
